//
//  Register.m
//  Opper
//
//  Created by Le Dung on 4/12/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "Register.h"

@interface Register ()

@end

@implementation Register

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [StyleClass barButtonItemEdit:self left:YES right:NO];
}

-(BOOL)shouldAutorotate
{
    return NO;
}
-(void)leftPress
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
    [StyleClass createNavi:self andTitle:@"Register"];
    [StyleClass background:self.view];
    [self styleContaint];
    [self buttonTarget];
 
}

-(void)styleContaint
{
    self.containView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.containView.layer.shadowOffset = CGSizeMake(0.0, 2.0);
    self.containView.layer.shadowOpacity = 0;
}

-(void)buttonTarget
{
    [self.registerButton addTarget:self action:@selector(userRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton setBackgroundImage:[StyleClass nRed] forState:UIControlStateNormal];
    
    [self.eMail setBackground:[StyleClass nText]];
    [self.passWord setBackground:[StyleClass nText]];
    [self.passComfirm setBackground:[StyleClass nText]];
    self.eMail.delegate=self;
    self.passWord.delegate=self;
    self.passComfirm.delegate=self;
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField==self.passComfirm)
    {
        if(![self.passWord.text isEqual:self.passComfirm.text])
        {
            [StyleClass warningShow:nil andMassage:@"PassConFirm was wrong, please try again !"];
        }
        
    }
    [textField resignFirstResponder];
    return YES;
}
-(void)userRegister
{
    
    if(![self.eMail.text isEqualToString:@""] && ![self.passWord.text isEqualToString:@""] && ![self.passComfirm.text isEqualToString:@""])
    {
        NSMutableDictionary *dictionary=[[NSMutableDictionary alloc]init];
        [dictionary  setValue:self.eMail.text forKey:@"email"];
        [dictionary setValue:self.passWord.text forKey:@"password"];
        NSString *jsonResponse =[GetData sendRequest:@"register" andDictionary:dictionary];
        
        if(jsonResponse)
        {
            if(![GetData checkError:jsonResponse])
            {
                
                Login *page =[[Login alloc]init];
                page.accountTemp =[[[AccountTemp alloc]init]autorelease];
                page.accountTemp.userTemp =self.eMail.text;
                page.accountTemp.passTemp=self.passWord.text;
                [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
                
                
            }
            else
            {
                [StyleClass warningShow:nil andMassage:[GetData  checkMassage:jsonResponse]];
            }
     
        }
        
        [dictionary release];
    
    }
    
    else
    {
        [StyleClass warningShow:nil andMassage:@"Error !"];
    }
}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_registerButton release];
    [_eMail release];
    [_passWord release];
    [_passComfirm release];
    [_containView release];
    [super dealloc];
}
@end
